package com.kkb.service.impl;

import com.kkb.domain.AjaxResult;
import com.kkb.mapper.TbSysuserMapper;
import com.kkb.mapper.TbSysuserRoleMapper;
import com.kkb.mapper.complex.SysUserMapper;
import com.kkb.pojo.*;
import com.kkb.pojo.complex.MenuItem;
import com.kkb.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private TbSysuserMapper sysuserMapper;
    @Autowired
    private SysUserMapper userMapper;


    public List<TbSysuser> getUserList() {
        TbSysuserExample example = new TbSysuserExample();
        TbSysuserExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo("N");
        List<TbSysuser> userList = sysuserMapper.selectByExample(example);
        return userList;
    }

    public TbSysuser getUserInfo(String username) {
        TbSysuserExample example = new TbSysuserExample();
        TbSysuserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<TbSysuser> list = sysuserMapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return null;
        }
        TbSysuser user = list.get(0);
        return user;
    }

    public List<MenuItem> getUserMenu(String username) {
        return getMenuList(username, -1);
    }

    public List<MenuItem> getMenuList(String username, int parentId) {
        Map param = new HashMap();
        param.put("username", username);
        param.put("parentId", parentId);
        List<MenuItem> list = userMapper.getUserMenuList(param);
        if (list != null && list.size() >0) {
            list.forEach(m->m.setChildren(getMenuList(username, m.getAuthorityId())));
            return list;
        }
        return null;
    }

    /**
     * 通过id获取user对象
     * @param userId
     * @return
     */
    public TbSysuser getUserById(Integer userId) {
        TbSysuser user = sysuserMapper.selectByPrimaryKey(userId);
        if (user == null) {
            return null;
        }
        return user;
    }

    @Override
    public boolean updateUser(TbSysuser sysuser) {
        TbSysuser user = sysuserMapper.selectByPrimaryKey(sysuser.getSysuserId());
        user.setUpdateTime(new Date());
        user.setMobile(sysuser.getMobile());
        user.setName(sysuser.getName());
        user.setStatus(sysuser.getStatus());
        user.setUsername(sysuser.getUsername());

        int i = sysuserMapper.updateByPrimaryKey(user);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public String checkLoginNameUnique(String username) {
        return null;
    }

    @Override
    public boolean changeStatus(TbSysuser user) {
        TbSysuser sysUser = sysuserMapper.selectByPrimaryKey(user.getSysuserId());
        //更改用户状态，启用改为禁用
        if("1".equals(user.getStatus())){
            sysUser.setStatus("0");
        }else{
            //禁用改为启用
            sysUser.setStatus("1");
        }
        int result = sysuserMapper.updateByPrimaryKey(sysUser);
        if(result>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUserById(Integer sysUserId) {
        TbSysuser user = sysuserMapper.selectByPrimaryKey(sysUserId);
        user.setUpdateTime(new Date());
        //软删除用户，是否删除置为Y
        user.setIsDelete("Y");
        int i = sysuserMapper.updateByPrimaryKey(user);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean addUser(TbSysuser sysUser) {
        sysUser.setCreateTime(new Date());
        sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes()));
        sysUser.setIsDelete("N");
        int result = sysuserMapper.insert(sysUser);
        if(result>0){
            return true;
        }
        return false;
    }

    @Override
    public AjaxResult batchDeleteUser(List<Integer> ids) {
       String msg="";
        for (int id : ids) {
            TbSysuser user = sysuserMapper.selectByPrimaryKey(id);
            user.setUpdateTime(new Date());
            //软删除用户，删除置为Y
            user.setIsDelete("Y");
            int i = sysuserMapper.updateByPrimaryKey(user);
            if(i<1){
                msg+="sysUserId为"+id+"的用户删除失败！";
            }
        }
        if("".equals(msg)){
            return AjaxResult.success();
        }
        return AjaxResult.error(msg);
    }

    @Override
    public AjaxResult resetPwd(TbSysuser user) {
        TbSysuser sysUser = sysuserMapper.selectByPrimaryKey(user.getSysuserId());
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        sysUser.setPassword(md5Password);
        sysUser.setUpdateTime(new Date());
        int result = sysuserMapper.updateByPrimaryKey(sysUser);
        if(result>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
